package xcmg.device.web.prepPay;

import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;
import xcmg.device.infra.BasicController;
import xcmg.device.infra.ErrorCode;
import xcmg.device.service.prepPay.PrepPayService;
import xcmg.device.service.vo.prepPay.PrepPayAddVO;
import xcmg.device.service.vo.prepPay.PrepPayDiscardVO;
import xcmg.device.service.vo.prepPay.PrepPayQueryVO;
import yb.ecp.fast.infra.annotation.FastMappingInfo;
import yb.ecp.fast.infra.infra.ActionResult;
import yb.ecp.fast.infra.infra.PageCommonVO;
import yb.ecp.fast.infra.infra.SearchCommonVO;

import javax.validation.Valid;
import java.util.Arrays;

/**
 * @author: xiali@bkrwin.com
 * @description:
 * @date： 2019/6/6 9:30
 **/

@RestController
@RequestMapping("/prepPay")
public class PrepPayController  extends BasicController {
    @Autowired
    PrepPayService prepPayService;

    @RequestMapping(value = "/add",method = RequestMethod.POST)
    @ApiOperation(value = "新增结算卡记录")
    @FastMappingInfo(needLogin = true)
    public ActionResult add(@Valid @RequestBody PrepPayAddVO prepPayAddVO,BindingResult bindingResult)throws Exception {
        if(bindingResult.hasErrors() ){
            return actionResultWithBindingResult(ErrorCode.Failure,bindingResult);
        }
        ErrorCode  ret=prepPayService.add(prepPayAddVO,getUserId(),getOrgId());
        return actionResult(ret);
        }



    @RequestMapping(value = "/list",method = RequestMethod.POST)
    @ApiOperation(value = "结算卡列表")
    @FastMappingInfo(needLogin = true)
    public ActionResult list(@RequestBody SearchCommonVO<PrepPayQueryVO> condition) throws Exception {
        PrepPayQueryVO prepPayQueryVO=condition.getFilters();
        if(prepPayQueryVO==null){
            prepPayQueryVO=new PrepPayQueryVO();
        }
        prepPayQueryVO.setOrgId(getOrgId());
        condition.setFilters(prepPayQueryVO);
        PageCommonVO pageCommonVO=prepPayService.list(condition);
        return actionValueResult(pageCommonVO.getPageInfo());
    }

    @RequestMapping(value = "/discard",method = RequestMethod.POST)
    @ApiOperation(value = "作废结算卡记录")
    @FastMappingInfo(needLogin = true)
    public ActionResult discard(@Valid @RequestBody PrepPayDiscardVO prepPayDiscardVO,BindingResult bindingResult)throws Exception {
        if(prepPayDiscardVO==null ){
            return actionResult(ErrorCode.IllegalArument,bindingResult.getAllErrors().get(0).getDefaultMessage());
        }
        ErrorCode  ret;
        try{
            ret = prepPayService.discard(prepPayDiscardVO,getUserId(),getOrgId());
        }catch (Exception e){
            return new ActionResult(ErrorCode.Failure.getCode(), e.getMessage());
        }
        return actionResult(ret);
    }


    @RequestMapping(value = "/export",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public void export( String[] ids)throws Exception {
        prepPayService.export(Arrays.asList(ids));
    }

    @RequestMapping(value = "/exportAll",method = RequestMethod.POST)
    @FastMappingInfo(needLogin = true)
    public void exportAll(PrepPayQueryVO condition)throws Exception {
        condition.setOrgId(getOrgId());
        prepPayService.exportAll(condition);
    }

    @ApiOperation(value = "结算卡审核通过")
    @RequestMapping(value = "/audit",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult audit(String id){
        return actionResult(prepPayService.audit(id,true));
    }

    @ApiOperation(value = "结算卡详情")
    @RequestMapping(value = "/item",method = RequestMethod.GET)
    @FastMappingInfo(needLogin = true)
    public ActionResult item(String id){
        return actionValueResult(prepPayService.item(id));
    }
}
